<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->
    <title>Bootstrap 101 Template</title>

    <!-- Bootstrap -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
    <style>
        body{
            padding-left: 14px;
            padding-top: 14px;
        }
        body{
            padding: 0 auto;
        }
        ul,li{
            list-style: none;
            padding: 0;
        }
        li{
            padding-bottom: 16px;
        }
        a,a:link,a:visited,a:hover,a:active{
            text-decoration: none;
        }
        .highLight{
            color: red;
        }
    </style>
</head>
<body>
<div>
    <input type="text" class="form-control" style="display:inline;width: 50%;" id="news-tips">
    <button type="submit" class="btn btn-primary" onclick="beginSearch()">搜索一下</button>
</div>
<hr>
<div>
    <ul id="queryResult">
<!--        <li>-->
<!--            <h4><a href="http://www.baidu.com">百度</a> </h4>-->
<!--            <p>啊实打实十大<span class="highlight">刘</span>阿松大 </p>-->
<!--        </li>-->
<!--        <li>-->
<!--            <h4><a href="http://www.baidu.com">百度</a> </h4>-->
<!--            <p>的方式大多数<span class="highlight">刘</span>阿萨大多数 </p>-->
<!--        </li>-->
    </ul>
</div>
<script>
    $('#news-tips').autocomplete({
        source: 'http://localhost:8080/news/tips',
        delay:100,
        minLength:1
        // messages: {
        //     noResults: '',
        //     results: function() {}
        // }
    })

    function beginSearch() {
        var searchText=$('#news-tips').val();

        if (searchText.trim()){
            $.get('http://localhost:8080/news/search?text='+searchText.trim(),{},function (data) {
                renderSearchResult(data);
            },'json');
        }
    }
    function renderSearchResult(data) {
        var ul=document.getElementById('queryResult');
        ul.innerHTML='';
        for (var i = 0; i < data.length; i++) {
            var singleResult=data[i];
            var url=singleResult.url;
            var title=singleResult.title;
            var content=singleResult.content;

            var li=document.createElement('li');
            var h4=document.createElement('h4');
            var a=document.createElement('a');
            var p=document.createElement('p');

            a.setAttribute("href",url);
            a.setAttribute("target",'_blank');
            a.innerHTML=title;
            p.innerHTML=content;

            h4.appendChild(a);
            li.appendChild(h4);
            li.appendChild(p);

            ul.appendChild(li);
        }
    }
</script>
</body>
</html>